package com.koron.configure.workflow.service.impl;

import com.koron.common.core.business.workflow.bean.po.ConfigureWorkflow;
import com.koron.common.core.business.workflow.bean.properties.WorkflowPlugin;
import com.koron.common.core.business.workflow.bean.properties.WorkflowProperties;
import com.koron.common.core.config.ThreadLocalContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.stereotype.Service;

/**
 * 内部工作流插件接口
 * @author jzp
 * @since 2022/1/11
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class WorkflowService {
    private final ConfigureWorkflowServiceImpl configureWorkflowService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    public WorkflowProperties getWorkflowProperties(String processCode) {
        ConfigureWorkflow configureWorkflow = ADOConnection.runTask(getCurrDs(), configureWorkflowService, "getWorkflow", ConfigureWorkflow.class, processCode);
        return WorkflowProperties.builder()
                .appCode(configureWorkflow.getAppCode())
                .processCode(configureWorkflow.getProcessCode())
                .setCode(configureWorkflow.getSetCode())
                .moduleCode(configureWorkflow.getModuleCode())
                .callbackController(configureWorkflow.getCallbackController()).build();
    }

    /**
     * 根据工作流节点信息找到对应插件
     * @param processCode processCode
     * @param nodeCode nodeCode
     * @param type
     *      0 选人组件过滤
     *      1 节点出口人员过滤
     *      2 节点入口人员过滤
     * @return WorkflowPlugin对象
     */
    public WorkflowPlugin getPluginUrlByNode(String processCode, String nodeCode, Integer type) {
        return ADOConnection.runTask(getCurrDs(), configureWorkflowService, "getValidPlugin", WorkflowPlugin.class, processCode, nodeCode, type);
    }

}
